using System;
using System.Collections.Generic;
using System.Text;

namespace Gestion.Domain.Base
{
    [Serializable]
    public abstract class BaseDomainEntity
    {
        public abstract Object GetId();

        public virtual Object GetIdForCompare()
        {
            return GetId();
        }

        /// <summary>
        /// Equals
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public override bool Equals(object other)
        {
            if (this == other)
                return true;
            if (!GetType().IsAssignableFrom(other.GetType()) && !other.GetType().IsAssignableFrom(GetType()))
                return false;

            BaseDomainEntity otherAsDomain = other as BaseDomainEntity;

            if (otherAsDomain == null)
                return false;

            if ((GetIdForCompare() == null) || (otherAsDomain.GetIdForCompare() == null))
                return false;

            return GetIdForCompare().Equals(otherAsDomain.GetIdForCompare());
        }

        /// <summary>
        /// GetHashCode
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            unchecked
            {
                int result;
                result = GetIdForCompare().GetHashCode();
                result = 29 * result;
                return result;
            }
        }
    }
}
